package com.yaodian100.ec.payment.web;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Collection;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.yaodian100.core.common.dao.impl.CommonCriteria;
import com.yaodian100.core.common.entity.SimplePager;
import com.yaodian100.core.common.exception.CoreException;
import com.yaodian100.core.common.service.DomainService;
import com.yaodian100.core.common.utils.DateUtil;
import com.yaodian100.core.menu.entity.Menu;
import com.yaodian100.ec.payment.entity.BankTransfer;
import com.yaodian100.ec.payment.service.TransferService;

/**
 * @author jinwei.lin
 * 
 */
public class BankPaymentAction extends ActionSupport implements Preparable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 548308224134647780L;

	/** logger */
	private final Logger logger = LoggerFactory.getLogger(BankPaymentAction.class);

	private Menu statusMenu; // 與 getter, setter
	private SimplePager pager;
	private DomainService<Menu> menuService; // 與 setter
	private TransferService transferService;

	// 查詢條件
	private String idParam;
	private String billDtStParam;
	private String billDtEdParam;
	private String statusParam;
	private String minDate;
	private String today;

	private String[] bankIdList;
	private String bktId;

	private String memo;
	private String orderId;

	public String getMemo() {
		return memo;
	}

	public void setMemo(String memo) {
		this.memo = memo;
	}

	public String getOrderId() {
		return orderId;
	}

	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}

	private BankTransfer bank;
	private Collection<BankTransfer> bankList;

	/**
	 * 檢視汇款资料
	 * 
	 * @return
	 */
	public String view() throws Exception {
		if (StringUtils.isNotBlank(bktId) || bank.getId() != null) {
			try {
				if (null != bktId) {
					bank = transferService.getBankTransfer(bktId);
				} else {
					bank = transferService.getBankTransfer(bank.getId());
				}
			} catch (CoreException e) {
				addActionError(e.getMessage());
				e.printStackTrace();
			}
		}
		return "view";
	}

	// 异动状态为待招领
	public String update() throws Exception {
		if (StringUtils.isNotBlank(bktId) || bank.getId() != null) {
			try {
				bank = transferService.getBankTransfer(bank.getId());
				if (bank.getStatus().equals("10") || bank.getStatus().equals("30")) {
					bank.setStatus("50");
					transferService.updateBankTransfer(bank);
					addActionMessage("储存成功");
				} else {
					addActionMessage("目前状态不可以异动");
				}
			} catch (CoreException e) {
				addActionError(e.getMessage());
				e.printStackTrace();
			}
		}
		return "view";
	}

	// 批次异动状态为待招领
	public String updateAll() throws Exception {
		if (bankIdList != null) {
			try {
				String mess = transferService.updateBankTransferList(bankIdList, "50");
				addActionMessage(mess);
			} catch (CoreException e) {
				addActionError(e.getMessage());
				e.printStackTrace();
			}
		}
		return query();
	}

	/**
	 * 增加备注
	 * 
	 * @return
	 * @throws Exception
	 */
	public String addMemo() throws Exception {
		if (StringUtils.isNotBlank(bktId) || bank.getId() != null) {
			try {
				if (null != bktId) {
					bank = transferService.getBankTransfer(bktId);
				} else {
					bank = transferService.getBankTransfer(bank.getId());
				}
				bank.setMemo(StringUtils.defaultString(bank.getMemo()) + memo);

				logger.info("memo:{}", memo);
				transferService.updateBankTransfer(bank);
			} catch (CoreException e) {
				addActionError(e.getMessage());
				e.printStackTrace();
			}
		}
		return "view";
	}

	/**
	 * 人工收款
	 * 
	 * @return
	 * @throws Exception
	 */
	public String charge() throws Exception {
		if (StringUtils.isNotBlank(bktId)) {
			try {

				transferService.chargeByBankTransfer(orderId, bktId);
				bank = transferService.getBankTransfer(bktId);
				// bank.setMemo(bank.getMemo() + memo);
				// transferService.updateBankTransfer(bank);
			} catch (CoreException e) {
				addActionError(e.getMessage());
				e.printStackTrace();
			}
		}
		return "view";
	}

	/**
	 * 以订单编号查询汇款資料列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public String queryByOrder() throws Exception {
		CommonCriteria crit = new CommonCriteria();
		crit.addEq("orderId", orderId);
		bankList = transferService.getBankTransferList(0, -1, crit, null);
		return "listByOrder";
	}

	/**
	 * 汇款資料列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public String query() throws Exception {
		CommonCriteria crit = new CommonCriteria();
		if (StringUtils.isNotBlank(idParam)) {
			crit.addRlike("id", idParam);
		} else {
			if (StringUtils.isBlank(billDtStParam)) {
				billDtStParam = DateUtil.convertDateToString("yyyy-MM-dd", DateUtil.getToday().getTime());
			}
			if (StringUtils.isBlank(billDtEdParam)) {
				billDtEdParam = DateUtil.convertDateToString("yyyy-MM-dd", DateUtil.getToday().getTime());
			}
			DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			crit.addGe("billDate", format.parse(billDtStParam + " 00:00:00"));
			crit.addLe("billDate", format.parse(billDtEdParam + " 23:59:59"));
		}
		if (StringUtils.isNotBlank(statusParam)) {
			crit.addEq("status", statusParam);
		}

		if (pager == null) {
			pager = new SimplePager();
		}
		// pager.setPageRecord(1);
		int entitySize = transferService.getBankTransferListSize(crit).intValue();
		pager.setTotalSize(entitySize);

		bankList = transferService.getBankTransferList(pager.getCurrentPage() * pager.getPageRecord(), pager
				.getPageRecord(), crit, null);
		logger.info("pager:{}", pager);

		return "list";
	}

	/**
	 * 查询
	 * 
	 * @return
	 * @throws Exception
	 */
	public String search() throws Exception {
		return "search";
	}

	public void prepare() throws Exception {
		clearErrorsAndMessages();

		if (org.apache.commons.lang.StringUtils.isBlank(billDtStParam)) {
			billDtStParam = DateUtil.convertDateToString("yyyy-MM-dd", DateUtil.getToday().getTime());
		}
		if (org.apache.commons.lang.StringUtils.isBlank(billDtEdParam)) {
			billDtEdParam = DateUtil.convertDateToString("yyyy-MM-dd", DateUtil.getToday().getTime());
		}

		if (StringUtils.isBlank(minDate)) {
			minDate = DateUtil.convertDateToString("yyyy,MM - 1,dd", DateUtil.getTodayAddDays(-60).getTime());
		}
		if (StringUtils.isBlank(today)) {
			today = DateUtil.convertDateToString("yyyy,MM - 1,dd", DateUtil.getToday().getTime());
		}

		if (statusMenu == null) {
			statusMenu = menuService.get("bankTransfer.status");
		}

	}

	public Menu getStatusMenu() {
		return statusMenu;
	}

	public void setStatusMenu(Menu statusMenu) {
		this.statusMenu = statusMenu;
	}

	public SimplePager getPager() {
		return pager;
	}

	public void setPager(SimplePager pager) {
		this.pager = pager;
	}

	public String getIdParam() {
		return idParam;
	}

	public void setIdParam(String idParam) {
		this.idParam = idParam;
	}

	public String getBillDtStParam() {
		return billDtStParam;
	}

	public void setBillDtStParam(String billDtStParam) {
		this.billDtStParam = billDtStParam;
	}

	public String getBillDtEdParam() {
		return billDtEdParam;
	}

	public void setBillDtEdParam(String billDtEdParam) {
		this.billDtEdParam = billDtEdParam;
	}

	public String getStatusParam() {
		return statusParam;
	}

	public void setStatusParam(String statusParam) {
		this.statusParam = statusParam;
	}

	public String getMinDate() {
		return minDate;
	}

	public void setMinDate(String minDate) {
		this.minDate = minDate;
	}

	public String getToday() {
		return today;
	}

	public void setToday(String today) {
		this.today = today;
	}

	public String getBktId() {
		return bktId;
	}

	public void setBktId(String bktId) {
		this.bktId = bktId;
	}

	public BankTransfer getBank() {
		return bank;
	}

	public void setBank(BankTransfer bank) {
		this.bank = bank;
	}

	public Collection<BankTransfer> getBankList() {
		return bankList;
	}

	public void setBankList(Collection<BankTransfer> bankList) {
		this.bankList = bankList;
	}

	public void setMenuService(DomainService<Menu> menuService) {
		this.menuService = menuService;
	}

	public void setTransferService(TransferService transferService) {
		this.transferService = transferService;
	}

	public String[] getBankIdList() {
		return bankIdList;
	}

	public void setBankIdList(String[] bankIdList) {
		this.bankIdList = bankIdList;
	}

}